<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>商品上架</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
		
	</head>
	<body>
		<table id="onselltable" lay-filter="onselltable"></table>
		<script type="text/html" id="onsellact">
			<a class="layui-btn layui-btn-sm" lay-event="onsell">上架</a>
		</script>
		<script src="../../layuiadmin/layui/layui.js"></script>  
		<script src="../../js/jquery-1.8.3.js"></script>
		<script src="../../js/jquery.cookie.js"></script>
		<script src="../../js/user/common.js"></script>
		<script>
			layui.use(['table','layer','jquery'],function(){
				var table = layui.table,
				layer = layui.layer,
				$ = layui.$;
				
				table.render({
					elem:'#onselltable',
					url:urlHead2 + '/room/getRoomList',//数据接口
					page:true,//开启分页
                    headers:{
                        "Authorization":$.cookie('access_token')
                    },
					cols:[[//表头
						{field:'roomname',title:'房间名称',fixed:'left',width:120},
						{field:'sourceid',title:'房间来源',width:90},
						{field:'count',title:'房间数量',width:90},
						{field:'roomdrc',title:'房间描述',width:100},
						{field:'batchcode',title:'上架批次',width:100},
						{field:'num',title:'上架房间数',edit:'text',width:104},
						{field:'begintime',title:'兑换开始时间',width:117,edit:'text'},
						{field:'endtime',title:'兑换截止时间',width:117,edit:'text'},
						{field:'rate',title:'兑换比例',edit:'text',width:90},
						{fixed:'right',title:'操作',toolbar:'#onsellact',width:80}
					]]
				});
		
				
				//监听工具条
				table.on('tool(onselltable)',function(obj){
					var data = obj.data;//获得当前行数据
					var layEvent = obj.event; //获得 lay-event 对应的值
					var tr = obj.tr; //获得当前行 tr 的DOM对象
					console.log(data);
					console.log(layEvent);
					console.log(tr);
					
					var Data =[{
						"batchcode":data.batchcode,
						"num":data.num,
						"begintime":data.begintime,
						"endtime":data.endtime,
						"rate":data.rate
					}]
					
					if(layEvent === 'onsell'){//上架操作
						//所填项不能为空
						if(data.num==null){
							layer.msg('上架房间数不能为空');
							return false;
						}else if(data.begintime==null){
							layer.msg('兑换开始时间不能为空');
							return false;
						}else if(data.endtime==null){
							layer.msg('兑换截止时间不能为空');
							return false;
						}else if(data.rate==null){
							layer.msg('兑换比例不能为空');
							return false;
						}
						
						//判断上架房间数
						if(data.num>0&&data.num<=data.count){
							$.ajax({
								type:"post",
								url:urlHead2 + '/killup/killupCarriage',//数据接口
								async:true,
								contentType: "application/json; charset=utf-8",
								headers:{
					        		"Authorization":$.cookie("access_token")
					   			},
								data: JSON.stringify(Data),
								success:function(res){
									if(res.code==0){//上架成功
										layer.msg('上架成功',function(){
											tr.html('');
										});
									}else if(res.code==600602){
										layer.msg('用户未登录，请先登录',function(){
											location.replace('../../views/user/login_by_username_new.html');
										});
									}else if(res.code==600603){
										layer.msg('token已过期，请重新登录',function(){
											location.replace('../../views/user/login_by_username_new.html');
										});
									}else{
										layer.msg(res.msg);
									}
								},
								error:function(){
									location.href = "../error.html";
								},
								dataType:"json"
							});
						}else{//上架房间数大于房间数量
							layer.msg('上架房间数有误，请重新输入');
						}
						
					}
				});
			});
		</script>
	</body>
</html>
